package cn.wolfcode.service.impl;

import cn.wolfcode.cache.IUserInfoRedisService;
import cn.wolfcode.domain.UserInfo;
import cn.wolfcode.exception.LogicException;
import cn.wolfcode.repository.UserInfoRepository;
import cn.wolfcode.service.IUserInfoService;
import cn.wolfcode.util.AssertUtil;
import cn.wolfcode.util.Consts;
import cn.wolfcode.util.RedisKeys;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.UUID;

@Service
public class UserInfoServiceImpl implements IUserInfoService {

    @Autowired
    private UserInfoRepository repository;
    @Autowired
    private IUserInfoRedisService userInfoRedisService;

    @Override
    public void insert(UserInfo user) {
        repository.save(user);
    }

    @Override
    public void update(UserInfo user) {
        repository.save(user);
    }

    @Override
    public void delete(String id) {
        repository.deleteById(id);
    }

    @Override
    public UserInfo get(String id) {
        return repository.findById(id).orElse(null);
    }

    @Override
    public List<UserInfo> listAll() {
        return repository.findAll();
    }

    @Override
    public boolean checkPhone(String phone) {
        UserInfo byPhone = repository.findByPhone(phone);//根据手机号去数据库查询是否已经存在
        return byPhone != null;
    }

    @Override
    public void sendVerifyCode(String phone) {
        String code = UUID.randomUUID().toString().replaceAll("-","").substring(0, 4);

        StringBuilder builder = new StringBuilder(50);
        builder.append("你的验证码为:").append(code).append(",该验证码会在")
                .append(Consts.VERIFY_CODE_VAI_TIME).append("分钟后失效");
        System.out.println(builder.toString());//模拟发送验证码
//        String url = "Url:https://way.jd.com/chuangxin/dxjk?" +
//                "mobile="+phone+"&content=【创信】"+builder.toString()+
//                "&appkey=fbaf2afb58fb0c05240044c1e525b418";
//        RestTemplate template = new RestTemplate();
//        String ret = template.getForObject(url, String.class);
//        System.out.println(ret);
//        if (!ret.contains("Success")){
//            throw new LogicException("短信发送失败");
//        }

        //把手机号和验证码存入redis数据库中
        userInfoRedisService.setVerfyCode(phone,code);

    }

    @Override
    public void regist(String phone, String nickname, String password, String rpassword, String verifyCode) {
        AssertUtil.haslength(phone,"手机号不能为空");
        AssertUtil.haslength(nickname,"用户名不能为空");
        AssertUtil.haslength(password,"密码不能为空");
        AssertUtil.haslength(rpassword,"确认密码不能为空");
        AssertUtil.haslength(verifyCode,"验证码不能为空");
        //判断两次密码是否正确
        AssertUtil.isEquals(password,rpassword,"两次输入的密码不匹配");
        //判断手机号是否唯一
        if (this.checkPhone(phone)){
            throw new RuntimeException("此手机号已被注册");
        }
        //判断验证码是否正确
        String code = userInfoRedisService.getVerifyCode(phone);
        if (code==null || !code.equalsIgnoreCase(verifyCode)){
            throw new LogicException("验证码错误");
        }

        //注册
        UserInfo userInfo = new UserInfo();
        userInfo.setNickname(nickname);
        userInfo.setPhone(phone);
        userInfo.setEmail("");
        userInfo.setHeadImgUrl("/images/default.png");
        userInfo.setPassword(password);
        userInfo.setLevel(1);
        userInfo.setCity("");
        userInfo.setInfo("");
        userInfo.setState(UserInfo.STATE_NORMAL);
        this.insert(userInfo);
    }

    @Override
    public UserInfo login(String username, String password) {
        UserInfo userInfo = repository.findByPhone(username);
        if (userInfo == null || !password.equals(userInfo.getPassword())){
            throw new LogicException("账号或密码错误");
        }
        return userInfo;
    }



}
